<template>
  <div>
    <el-row :gutter="20" style="padding: 20px">
      <el-col :span="12">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
          <el-form-item label="手机号">
            <el-input v-model="username" readonly></el-input>
          </el-form-item>
          <el-form-item v-if="!walletPassSatus" prop="smsCaptchaResult" label="短信验证码">
            <el-input style="width: 180px; margin-right: 10px" prefix-icon="el-icon-chat-dot-square" v-model="ruleForm.smsCaptchaResult" placeholder="短信验证码"></el-input>
            <el-button type="primary" v-if="!countDown" @click="sendVerifyCode">发送</el-button>
            <el-button type="primary" :disabled="true" v-else>{{ countDown }}s</el-button>
          </el-form-item>
          <el-form-item label="新密码" prop="walletPassword">
            <el-input :maxlength="6" placeholder="请输入6位支付密码" v-model="ruleForm.walletPassword" auto-complete="new-password" type="password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="conWalletPassword">
            <el-input :maxlength="6" placeholder="请输入确认6位支付密码" v-model="ruleForm.conWalletPassword" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-dialog title="提示" :visible.sync="isExamine" width="450px" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <div>
        <!-- <i class="el-icon-success"></i>  -->
        <p>您的账户还未审核通过，请耐心等待</p>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="() => $router.go(-1)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  <script>
  const reg5 = /^\d*\.?\d+$/;
export default {
  data() {
    return {
      isExamine: false,
      walletPassSatus: false,
      isSuccess: false,
      uuid: "",
      smsUuid: "",
      codeUrl: "",
      username: localStorage.getItem("username"),
      countDown: 0, // 倒计时
      ruleForm: {
        walletPassword: "",
        conWalletPassword: "",
        smsCaptchaResult: "",
        uuid: "",
      },
      rules: {
        walletPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          {
            validator: (rule, value, cb) => this.checkNum(rule, value, cb),
          },
        ],
        conWalletPassword: [
          { required: true, message: "请输入确认密码", trigger: "blur" },
          {
            validator: (rule, value, cb) => this.checkNum(rule, value, cb),
          },
        ],
        smsCaptchaResult: [
          {
            required: true,
            message: "请输入手机验证码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    checkNum(rule, value, cb) {
      if (!reg5.test(value)) {
        cb("请输入数字");
      }
      cb();
    },
    onSuccess() {
      this.isSuccess = true;
    },
    // 4.获取手机验证码
    async sendVerifyCode() {
      const params = {
        uuid: this.uuid,
      };
      // 4.2 获取短信验证码
      this.$post("/customer/wallet/updatePwdOfSms", params)
        .then((res) => {
          if (res && res.code * 1 === 200) {
            this.smsUuid = res.data;
            this.$message.success("短信发送成功");
            this.countDown = 60;
            this.timeIntervalID = setInterval(() => {
              this.countDown--;
              // 4.1 如果减到0 则清除定时器
              if (this.countDown == 0) {
                clearInterval(this.timeIntervalID);
              }
            }, 1000);
          } else {
            //
          }
        })
        .catch(() => {
          //
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = {
            walletPassword: this.ruleForm.walletPassword,
            conWalletPassword: this.ruleForm.conWalletPassword,
          };
          if (!this.walletPassSatus) {
            params.code = this.ruleForm.smsCaptchaResult;
            params.uuid = this.smsUuid;
          }
          const url = !this.walletPassSatus
            ? "/customer/wallet/updatePwd"
            : "/customer/wallet/setPwd";
          this.$post(url, params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message.success("操作成功");
                this.ruleForm = {
                  walletPassword: "",
                  conWalletPassword: "",
                  smsCaptchaResult: "",
                  uuid: "",
                };
                this.getWallerData();
              }
            })
            .finally(() => {
              clearInterval(this.timeIntervalID);
              this.countDown = 0;
            });
        } else {
          return false;
        }
      });
    },
    getWallerData() {
      this.$post("/customer/wallet/info").then((res) => {
        if (res && res.code * 1 === 200) {
          this.walletPassSatus = !res.data.walletPassSatus;
        } else {
          this.isExamine = true;
          this.walletPassSatus = false;
        }
      });
    },
  },
  mounted() {
    this.getWallerData();
  },
};
</script>
<style scoped lang="less">
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
</style>